<template>
  <div class="music-player" :class="{ 'is-playing': isPlaying }">
    <!-- 主播放器 -->
    <div class="player-main">
      <!-- 封面旋转动画 -->
      <div class="cover-wrapper">
        <div class="cover-rotate" :class="{ 'rotating': isPlaying }">
          <img :src="currentSong.cover" :alt="currentSong.name">
        </div>
        <div class="cover-mask"></div>
      </div>

      <!-- 歌曲信息 -->
      <div class="song-info">
        <div class="song-name-wrapper">
          <h3 class="song-name">{{ currentSong.name }}</h3>
          <span class="song-artist">{{ currentSong.artist }}</span>
        </div>
        <div class="song-lrc">{{ currentLyric }}</div>
      </div>

      <!-- 播放控制 -->
      <div class="player-controls">
        <!-- 播放进度 -->
        <div class="progress-bar" @click="handleProgressClick">
          <div class="progress-loaded" :style="{ width: loadedProgress + '%' }"></div>
          <div class="progress-played" :style="{ width: playProgress + '%' }"></div>
          <div class="progress-handle" :style="{ left: playProgress + '%' }"
               @mousedown="startDrag"
               @touchstart="startDrag">
          </div>
          <div class="time-info">
            <span class="time-current">{{ formatTime(currentTime) }}</span>
            <span class="time-total">{{ formatTime(duration) }}</span>
          </div>
        </div>

        <!-- 控制按钮 -->
        <div class="control-buttons">
          <div class="play-mode" @click="togglePlayMode">
            <i class="iconfont" :class="playModeIcon"></i>
          </div>
          <div class="prev-btn" @click="playPrev">
            <i class="iconfont icon-prev"></i>
          </div>
          <div class="play-btn" @click="togglePlay">
            <i class="iconfont" :class="isPlaying ? 'icon-pause' : 'icon-play1'"></i>
          </div>
          <div class="next-btn" @click="playNext">
            <i class="iconfont icon-next"></i>
          </div>
          <div class="volume-control">
            <i class="iconfont icon-volume" @click="toggleMute"></i>
            <div class="volume-slider" @click="handleVolumeClick">
              <div class="volume-progress" :style="{ width: volume + '%' }"></div>
              <div class="volume-handle" :style="{ left: volume + '%' }"></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 播放列表 -->
    <div class="playlist-drawer" :class="{ 'show': showPlaylist }">
      <div class="playlist-header">
        <h3>播放列表 ({{ playlist.length }})</h3>
        <i class="iconfont icon-close" @click="showPlaylist = false"></i>
      </div>
      <div class="playlist-content">
        <div v-for="(song, index) in playlist" 
             :key="song.id"
             class="playlist-item"
             :class="{ 'active': currentIndex === index }"
             @click="playSong(index)">
          <div class="item-info">
            <span class="item-name">{{ song.name }}</span>
            <span class="item-artist">{{ song.artist }}</span>
          </div>
          <div class="item-duration">{{ formatTime(song.duration) }}</div>
          <i class="iconfont icon-delete" @click.stop="removeSong(index)"></i>
        </div>
      </div>
    </div>

    <!-- 音频元素 -->
    <audio ref="audio" 
           :src="currentSong.url"
           @timeupdate="onTimeUpdate"
           @ended="onEnd"
           @canplay="onCanPlay">
    </audio>
  </div>
</template>

<script>
export default {
  name: 'MusicPlayer',
  data() {
    return {
      isPlaying: false,
      currentIndex: 0,
      currentTime: 0,
      duration: 0,
      volume: 80,
      loadedProgress: 0,
      showPlaylist: false,
      playMode: 'sequence', // sequence, random, single
      isDragging: false,
      currentLyric: '暂无歌词',
      playlist: [
        {
          id: 1,
          name: '示例音乐1',
          artist: '演唱者1',
          cover: 'https://picsum.photos/200/200?random=1',
          url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
          duration: 180
        },
        {
          id: 2,
          name: '示例音乐2',
          artist: '演唱者2',
          cover: 'https://picsum.photos/200/200?random=2',
          url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3',
          duration: 200
        },
        {
          id: 3,
          name: '示例音乐3',
          artist: '演唱者3',
          cover: 'https://picsum.photos/200/200?random=3',
          url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3',
          duration: 160
        }
      ]
    }
  },
  computed: {
    currentSong() {
      return this.playlist[this.currentIndex] || {}
    },
    playProgress() {
      return (this.currentTime / this.duration) * 100 || 0
    },
    playModeIcon() {
      const icons = {
        sequence: 'icon-sequence',
        random: 'icon-random',
        single: 'icon-single'
      }
      return icons[this.playMode]
    }
  },
  methods: {
    togglePlay() {
      if (this.isPlaying) {
        this.$refs.audio.pause()
      } else {
        this.$refs.audio.play()
      }
      this.isPlaying = !this.isPlaying
    },
    playNext() {
      if (this.playMode === 'random') {
        this.currentIndex = Math.floor(Math.random() * this.playlist.length)
      } else {
        this.currentIndex = (this.currentIndex + 1) % this.playlist.length
      }
      this.playCurrentSong()
    },
    playPrev() {
      if (this.playMode === 'random') {
        this.currentIndex = Math.floor(Math.random() * this.playlist.length)
      } else {
        this.currentIndex = this.currentIndex === 0 
          ? this.playlist.length - 1 
          : this.currentIndex - 1
      }
      this.playCurrentSong()
    },
    playCurrentSong() {
      this.$nextTick(() => {
        this.$refs.audio.play()
        this.isPlaying = true
      })
    },
    onTimeUpdate(e) {
      if (!this.isDragging) {
        this.currentTime = e.target.currentTime
      }
    },
    onEnd() {
      if (this.playMode === 'single') {
        this.$refs.audio.currentTime = 0
        this.$refs.audio.play()
      } else {
        this.playNext()
      }
    },
    onCanPlay(e) {
      this.duration = e.target.duration
    },
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60)
      const secs = Math.floor(seconds % 60)
      return `${mins}:${secs.toString().padStart(2, '0')}`
    },
    handleProgressClick(e) {
      const rect = e.target.getBoundingClientRect()
      const percent = (e.clientX - rect.left) / rect.width
      this.currentTime = this.duration * percent
      this.$refs.audio.currentTime = this.currentTime
    },
    startDrag(e) {
      this.isDragging = true
      document.addEventListener('mousemove', this.onDrag)
      document.addEventListener('mouseup', this.stopDrag)
      document.addEventListener('touchmove', this.onDrag)
      document.addEventListener('touchend', this.stopDrag)
    },
    onDrag(e) {
      if (!this.isDragging) return
      const rect = this.$el.querySelector('.progress-bar').getBoundingClientRect()
      const clientX = e.type.startsWith('touch') ? e.touches[0].clientX : e.clientX
      let percent = (clientX - rect.left) / rect.width
      percent = Math.min(1, Math.max(0, percent))
      this.currentTime = this.duration * percent
    },
    stopDrag() {
      if (!this.isDragging) return
      this.isDragging = false
      this.$refs.audio.currentTime = this.currentTime
      document.removeEventListener('mousemove', this.onDrag)
      document.removeEventListener('mouseup', this.stopDrag)
      document.removeEventListener('touchmove', this.onDrag)
      document.removeEventListener('touchend', this.stopDrag)
    },
    togglePlayMode() {
      const modes = ['sequence', 'random', 'single']
      const currentIndex = modes.indexOf(this.playMode)
      this.playMode = modes[(currentIndex + 1) % modes.length]
    },
    handleVolumeClick(e) {
      const rect = e.target.getBoundingClientRect()
      const percent = (e.clientX - rect.left) / rect.width * 100
      this.volume = Math.min(100, Math.max(0, percent))
      this.$refs.audio.volume = this.volume / 100
    },
    toggleMute() {
      this.$refs.audio.muted = !this.$refs.audio.muted
    },
    playSong(index) {
      this.currentIndex = index
      this.playCurrentSong()
    },
    removeSong(index) {
      this.playlist.splice(index, 1)
      if (index === this.currentIndex) {
        this.playNext()
      } else if (index < this.currentIndex) {
        this.currentIndex--
      }
    }
  }
}
</script>

<style scoped>
.music-player {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
  z-index: 1000;
  padding: 15px;
  transition: all 0.3s ease;
}

.player-main {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* 封面样式 */
.cover-wrapper {
  position: relative;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
}

.cover-rotate {
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.cover-rotate.rotating {
  animation: rotate 20s linear infinite;
}

@keyframes rotate {
  100% {
    transform: rotate(360deg);
  }
}

.cover-rotate img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cover-mask {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

/* 歌曲信息样式 */
.song-info {
  flex: 1;
  min-width: 0;
}

.song-name-wrapper {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 5px;
}

.song-name {
  font-size: 16px;
  font-weight: 500;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.song-artist {
  font-size: 14px;
  color: #666;
}

.song-lrc {
  font-size: 12px;
  color: #999;
}

/* 播放控制样式 */
.player-controls {
  flex: 2;
  min-width: 400px;
}

.progress-bar {
  position: relative;
  height: 4px;
  background: #eee;
  border-radius: 2px;
  cursor: pointer;
  margin-bottom: 15px;
}

.progress-loaded {
  position: absolute;
  height: 100%;
  background: rgba(255,107,107,0.2);
  border-radius: 2px;
}

.progress-played {
  position: absolute;
  height: 100%;
  background: #ff6b6b;
  border-radius: 2px;
}

.progress-handle {
  position: absolute;
  top: 50%;
  width: 12px;
  height: 12px;
  background: white;
  border: 2px solid #ff6b6b;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  transition: transform 0.3s ease;
}

.progress-handle:hover {
  transform: translate(-50%, -50%) scale(1.2);
}

.time-info {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.control-buttons {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 30px;
}

.control-buttons > div {
  cursor: pointer;
  transition: all 0.3s ease;
}

.control-buttons > div:hover {
  transform: scale(1.1);
}

.play-btn {
  width: 40px;
  height: 40px;
  background: #ff6b6b;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.play-btn i {
  color: white;
  font-size: 20px;
}

.prev-btn i, .next-btn i {
  font-size: 24px;
  color: #666;
}

/* 音量控制 */
.volume-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.volume-slider {
  width: 80px;
  height: 4px;
  background: #eee;
  border-radius: 2px;
  cursor: pointer;
  position: relative;
}

.volume-progress {
  position: absolute;
  height: 100%;
  background: #ff6b6b;
  border-radius: 2px;
}

.volume-handle {
  position: absolute;
  top: 50%;
  width: 10px;
  height: 10px;
  background: white;
  border: 2px solid #ff6b6b;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

/* 播放列表样式 */
.playlist-drawer {
  position: fixed;
  bottom: 80px;
  right: -400px;
  width: 400px;
  height: 500px;
  background: white;
  border-radius: 15px 0 0 0;
  box-shadow: -2px -2px 10px rgba(0,0,0,0.1);
  transition: right 0.3s ease;
}

.playlist-drawer.show {
  right: 0;
}

.playlist-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.playlist-content {
  height: calc(100% - 50px);
  overflow-y: auto;
}

.playlist-item {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.playlist-item:hover {
  background: #f5f5f5;
}

.playlist-item.active {
  color: #ff6b6b;
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-name {
  display: block;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-artist {
  font-size: 12px;
  color: #999;
}

.item-duration {
  font-size: 12px;
  color: #999;
  margin: 0 15px;
}

.icon-delete {
  opacity: 0;
  transition: opacity 0.3s ease;
}

.playlist-item:hover .icon-delete {
  opacity: 1;
}
</style> 